﻿$(function () {
    $('#navigation li').removeClass('active');
    $('#menu_selfRecord').addClass('active');
    twiSelf.init();
});


Twi.SCS.SelfRecord = {
    Vars: {}
     , init: function () {
         $('#btn_home').click(function () {
             if ($('#record_menu').css('height') == '0px') {
                 $('#record_menu').animate({
                     top: '-99px',
                     height: '100px',
                     opacity: 'show'
                 });
             }
             else {
                 twiSelf.recordMenuHide();
             }
         });
         $('#record_item_container').click(function () {
             twiSelf.recordMenuHide();
         });

         $('#dialog-message').dialog({
             autoOpen: false,
             width: 350,
             buttons: [{
                 text: '保存',
                 click: function () {
                     var id = $('#dialog-message').attr('sltItemID');
                     var remark = $('#textArea_Remark').val();
                     Twi.Ajax({
                         data:
                         {
                             cn: 'SingleRecord',
                             mn2: 'EditRemark',
                             id: id,
                             remark: remark
                         },
                         success: function (twi) {
                             var shortRemark = remark.length > 20 ? remark.substring(0, 20) + '...' : remark;
                             $('#' + id).children('.x_remark').html(shortRemark);
                             $('#dialog-message').dialog("close");
                         }
                     });
                 }
             }, {
                 text: '取消',
                 click: function () {
                     $(this).dialog("close");
                 }
             }]
         });

         twiSelf.menuItemAction();
         twiSelf.initDate();
         twiSelf.loadCheckedRecord();

     }

     , recordMenuHide: function () {
         $('#record_menu').animate({
             top: '0',
             height: '0',
             opacity: 'hide'
         });
     }

     , loadCheckedRecord: function () {
         Twi.Ajax({
             data:
             {
                 cn: 'SingleRecord',
                 mn2: 'GetMyRecordsByCondition',
                 startDate: $('#query_startDate').val(),
                 endDate: $('#query_endDate').val()
             },
             success: function (twi) {
                 if (twi.totalCount > 0) {
                     var list = JSON.parse(twi.data);
                     $('#record_item_container').html('');
                     for (var i = 0; i < list.length; i++) {
                         var itm = list[i];
                         itm.FRemark = itm.FRemark == null ? '' : itm.FRemark;
                         var shortRemark = itm.FRemark.length > 20 ? itm.FRemark.substring(0, 20) + '...' : itm.FRemark;
                         $('#record_item_container').append('<div class="record_item" id="' + itm.FID + '" FRemark="' + itm.FRemark + '"><span class="x_date">' + itm.FDate + '</span><span class="x_dayofweek">' + itm.FDayOfWeek + '</span><span class="x_time">' + itm.FTime + '</span><span class="x_remark">' + shortRemark + '</span><div class="btn_remark">修改备注</div></div>');
                         //修改备注事件
                         $('#' + itm.FID).children('.btn_remark').click(function () {
                             // twiSelf.showRemarkDialog(itm); //参数itm总是最后一条记录的
                             twiSelf.showRemarkDialog($(this));
                         });
                     }
                 } else {
                     $('#record_item_container').html('<div class="no_record">暂无记录</div>');
                 }
                 twiSelf.recordMenuHide();
             }
         });
     }
     , initDate: function () {
         //开始日期、结束日期
         $("#query_startDate").datepicker({
             changeMonth: true,
             altFormat: "yy-mm-dd",
             dateFormat: "yy-mm-dd",
             onSelect: function (dateText, inst) {
                 $("#query_endDate").datepicker("option", "minDate", dateText);
             }
         });
         $("#query_endDate").datepicker({
             changeMonth: true,
             altFormat: "yy-mm-dd",
             dateFormat: "yy-mm-dd",
             maxDate: new Date(),
             onSelect: function (dateText, inst) {
                 $("#query_startDate").datepicker("option", "maxDate", dateText);
             }
         });
         var tDay = new Date();
         tDay.setMonth(tDay.getMonth() - 1);
         $("#query_startDate").datepicker('setDate', tDay);
         $("#query_endDate").datepicker('setDate', new Date());
     }
     , menuItemAction: function () {
         $('#checkedRecord').click(function () {
             $('#record_head').html('打卡记录');
             $('#hiddenType').val('checkedRecord');
         });
         $('#outworkRecord').click(function () {
             $('#record_head').html('出差记录');
             $('#hiddenType').val('outworkRecord');
         });
         $('#sickRecord').click(function () {
             $('#record_head').html('请假记录');
             $('#hiddenType').val('sickRecord');
         });
         $('#outsideRecord').click(function () {
             $('#record_head').html('临时外出记录');
             $('#hiddenType').val('outsideRecord');
         });
         $('#restRecord').click(function () {
             $('#record_head').html('调休记录');
             $('#hiddenType').val('restRecord');
         });
         $('#btn_query').click(function () {
             var type = $('#hiddenType').val();
             if (type == "checkedRecord") {
                 twiSelf.loadCheckedRecord();
             }
         });
     }

     , showRemarkDialog: function (btn_remark) {
         var record = btn_remark.parent('.record_item');
         $('#dialog-message').dialog('open');
         $('#textArea_Remark').text(record.attr('FRemark'));
         $('#dialog-message').attr('sltItemID', record.attr('id')).dialog('option', 'title', record.children('.x_date').html() + ' ' + record.children('.x_dayofweek').html() + ' ' + record.children('.x_time').html() + ' 的打卡备注');
     }
};

var twiSelf = Twi.SCS.SelfRecord;
var twiVars = twiSelf.Vars;
